// pages/fee/fee.js

const app=getApp();
Page({

  /**
   * 页面的初始数据
   */
  data: {
    roomList:[],
    hasChoosen:false
  },
//caculate the total fee
totalFee(e){
  var inputData=e.detail.value,
      roomInfo=this.data.roomInfo;
  var waterFee=(inputData.newWaterNumber-roomInfo.waterNumber)*inputData.waterPrice,
      electricFee=(inputData.newElectricNumber-roomInfo.electricNumber)*inputData.electricPrice;
  var fee={}
  fee.waterFee=waterFee;
  fee.electricFee=electricFee;
  fee.useWaterNumber=inputData.newWaterNumber-roomInfo.waterNumber;
  fee.useElectricNumber=inputData.newElectricNumber-roomInfo.electricNumber;
  fee.newWaterNumber=inputData.newWaterNumber;
  fee.newElectricNumber=inputData.newElectricNumber
  wx.setStorageSync('fee', fee)
  wx.setStorageSync('roomInfo', roomInfo)
  wx.navigateTo({
    url: '/pages/fee/feeResult/feeResult',
  })
},

//make the picker's default value right
getThePickerValue(){
  var roomName=this.data.roomName,
      roomNameList=this.data.roomNameList,
      index;
  for(var i=0;i<roomNameList.length;i++){
    if(roomNameList[i]==roomName){
      index=i
    }
  }
  this.setData({
    index:index
  })
},

  //bind the roomname change
  roomChange(e){
    var index=e.detail.value,
        roomNameList=this.data.roomNameList;
    var roomName=roomNameList[index];
    this.setData({
      roomName:roomName
    })
    wx.setStorageSync('roomName', roomName)
    this.updateRoomInfo()
  },

  //the label has been clicked
  clickTheLabel(e){
    this.setData({
      roomName:e.detail
    })
    //get the roomInfo
    var roomName=this.data.roomName
    var roomInfo=this.getRoomInfoByName(roomName)
    this.setData({
      roomInfo:roomInfo
    })
    //跳转
    setTimeout(() => {
      this.setData({
        hasChoosen:true,
      })
    }, 200);
    //执行index的获取
    this.getThePickerValue()
  },

  //refresh the roomList
  reloadData(){
    //get the roomList
    wx.request({
      url: app.globalData.url,
      method:'GET',
      success:(res)=>{//!!!start the success!!!
        this.setData({
          roomList:res.data
        })
    //get the avatar
    var userAvatar=wx.getStorageSync('userAvatar')
    this.setData({
      userAvatar:userAvatar
    })
    this.makeLabelList();
    var roomNameList=this.makeRoomNameList(this.data.roomList);
    this.setData({
      roomNameList:roomNameList
    })
  },//!!!end the success!!!

    })
  },

  //make the roomNameList
  makeRoomNameList(roomList){
    var roomNameList=[];
    roomList.forEach((item)=>{
      roomNameList.push(item.roomName)
    })
    return roomNameList
  },

  //make the labelList
  makeLabelList(){
    var labelList=[];
    var roomlist=this.data.roomList;
    for(var i=0;i<roomlist.length;i++){
      var lableItem={};
      lableItem.roomName=roomlist[i].roomName;
      lableItem.isChecked=false;
      labelList.push(lableItem)
    }
    //set it to data
    this.setData({
      labelList:labelList
    })
  },

  //get roomInfo through roomName
  getRoomInfoByName(roomName){
    var roomInfo={}
    this.data.roomList.forEach((item)=>{
      if(item.roomName==roomName){
        roomInfo = item
      }
    })
    return roomInfo
  },

  //update the roomInfo by roomName
  updateRoomInfo(){
    setTimeout(() => {
      var roomInfo=this.getRoomInfoByName(wx.getStorageSync('roomName'))
      this.setData({
        roomInfo:roomInfo
      })
    }, 200);
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options) {
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow() {
    this.reloadData()
    this.updateRoomInfo()
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom() {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage() {

  }
})